/*
 * Same of SemanticListView
 */

package android.bluebox.view;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.bluebox.R;
import android.bluebox.model.StaticBox;
import android.bluebox.model.TagAdapter;
import android.bluebox.model.Tag;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class TagListView extends Activity { 

	ListView lvTag;
	ArrayList<Tag> tagList;
	TagAdapter cba;

	TextView txtNumberOfTags;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.taglist);

		StaticBox.dbBox.setContext(this);
		txtNumberOfTags = (TextView) findViewById(R.id.tl_number);

		lvTag = (ListView) findViewById(R.id.tl_list);
		cba = new TagAdapter(this, tagList);
		refreshTagList();
		lvTag.setOnItemClickListener(chooseTag);
	}

	public ArrayList<Tag> loadTag() {
		String strNumberOfTags = String.valueOf(StaticBox.dbBox.getNumberOfTags());
		txtNumberOfTags.setText(strNumberOfTags);
		ArrayList<Tag> list = StaticBox.dbBox.getAllTags();
		return list;
	}

	public void refreshTagList() {
		tagList = loadTag();
		cba.setArraylist(tagList);
		cba.notifyDataSetChanged();
		lvTag.setAdapter(cba);
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.taglist_optionmenu, menu);
		return true;
	}

	/*
	 * Create event for Option Menu
	 */
	public boolean onOptionsItemSelected(MenuItem item) {

		Intent intent;

		switch (item.getItemId()) {
		case R.id.tl_new_tag:

			AlertDialog.Builder alert = new AlertDialog.Builder(this);
			alert.setTitle("New Tag");
			alert.setMessage("Name");

			// Set an EditText view to get user Input
			final EditText edtName = new EditText(this);
			alert.setView(edtName);

			alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int button) {
					String newName = edtName.getText().toString().trim(); 
					if (newName.length() > 0) {
						createNewTag(newName);
					} else {
						Toast.makeText(getBaseContext(), "Please input tag name", Toast.LENGTH_SHORT).show();
					}
				}
			});

			alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int button) {
					// TODO Auto-generated method stub
				}
			});
			alert.show();
			break;

		case R.id.tl_change_ws:
			intent = new Intent(TagListView.this, WorkspaceListView.class);
			startActivity(intent);
			break;
		}
		
		
		return true;
	}

	public void createNewTag(String newName) {
		if (StaticBox.dbBox.getTag(newName) == null) {
			Tag tag = new Tag();
			tag.setName(newName);
			StaticBox.dbBox.insertTag(tag);
			refreshTagList();
		}
	}

	OnItemClickListener chooseTag = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> a, View view, int position, long id) {
			final Tag tag = (Tag) lvTag.getItemAtPosition(position);

			/*
			 * Create list of option: Connect, Edit, Delete
			 */
			final CharSequence[] tagOption = {"Open", "Send All", "Edit", "Delete"};

			/*
			 * Create dialog
			 */
			AlertDialog.Builder builder = new AlertDialog.Builder(TagListView.this);
			builder.setTitle(tag.getName() + " Options");
			builder.setItems(tagOption, new OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int item) {
					/*
					 * Open
					 */
					switch(item) {
					case 0:
						Intent intent = new Intent(TagListView.this, AValueListView.class);
						intent.putExtra("typeId", 1);
						intent.putExtra("filterId", tag.getId());	// Send tagId of tag you want to open
						startActivity(intent);
						break;
						
						/*
						 * Send all
						 */
					case 1:
						StaticBox.netBox.sendMessage(StaticBox.netBox.SENDMESSAGE,
								StaticBox.dbBox.getTag(tag.getId()).getName(), 
								StaticBox.dbBox.filterAValueByTag(tag.getId()));
						break;
						
						/*
						 * Edit
						 */
					case 2:
						editTagName(tag);
						break;

						/*
						 * Delete
						 */
					case 3:
						deleleTag(tag);
						break;
					}

				}

			});
			AlertDialog dialog = builder.create();

			/*
			 * Show dialog
			 */
			dialog.show();
		}
	};

	public void changeTagName(Tag tag, String newName) {
		tag.setName(newName);
		StaticBox.dbBox.updateTag(tag);
		refreshTagList();
	}

	public void deleleTag(Tag tag) {
		StaticBox.dbBox.deleteTag(tag.getId());
		refreshTagList();
	}

	public void editTagName(final Tag tag) {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle("Edit Tag");
		alert.setMessage("Tag Name");

		// Set an EditText view to get user Input
		final EditText edtName = new EditText(this);
		edtName.setText(tag.getName());
		alert.setView(edtName);

		alert.setPositiveButton("Change", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int button) {
				String newName = edtName.getText().toString().trim(); 
				if (newName.length() > 0) {
					changeTagName(tag, newName);
					refreshTagList();
				} else {
					Toast.makeText(getBaseContext(), "Fail create identity", Toast.LENGTH_SHORT).show();
				}
			}
		});

		alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int button) {
			}
		});
		alert.show();
	}
}
